Search for visually selected text
With this tip, you can select some text, then press a key to search for the next occurrence of the text. Two alternative methods are presented. Simple Nothing is needed if you want to search for the word under the cursor, just press *. To search for visually selected text, put this line in your vimrc: vnoremap // y/\V=escape(@",'/\') To use the mapping, visually select the characters that are wanted in the search, then type // to search for the next occurrence of the selected text. Then press n to search for the next occurrence. The :vnoremap () command maps // in visual mode to copy the visually selected text into the " buffer, then start a search command and paste an escaped version of the copied text into the line. represents Ctrl-R and represents carriage return (Enter). The search uses \V ( ) for "very no-magic" mode, and escape() ( ) to escape / and \ characters (the only ones that aren't literals with \V). Advanced The following is a more advanced implementation, with more robust functionality than the above map. Features *Press * to search forwards for selected text, or # to search backwards. *As normal, press n for next search, or N for previous. *Handles multiline selection and search. *Whitespace in the selection matches any whitespace when searching (searching for "hello world" will also find "hello" at the end of a line, with "world" at the start of the next line). *Each search is placed in the search history allowing you to easily repeat previous searches. *No registers are changed. Place the following mappings in your vimrc: " Search for selected text, forwards or backwards. vnoremap * : \let old_reg=getreg('"')let old_regtype=getregtype('"') \gvy/=&ic?'\c':'\C'=substitute( \escape(@", '/\.*$^~'\_s\+', '\\_s\\+', 'g') \gVzv:call setreg('"', old_reg, old_regtype) vnoremap # : \let old_reg=getreg('"')let old_regtype=getregtype('"') \gvy?=&ic?'\c':'\C'=substitute( \escape(@", '?\.*$^~['), '\_s\+', '\\_s\\+', 'g') \gVzv:call setreg('"', old_reg, old_regtype) Following is an alternative version with some extra features: *A global variable (g:VeryLiteral) controls whether selected whitespace matches any whitespace (by default, VeryLiteral is off, so any whitespace is found). *Type \vl to toggle VeryLiteral to turn whitespace matching off/on (assuming the default backslash leader key). *When VeryLiteral is off, any selected leading or trailing whitespace will not match newlines, which is more convenient, and avoids false search hits. Create file (for example) ~/.vim/plugin/vsearch.vim (Unix) or $HOME/vimfiles/plugin/vsearch.vim (Windows) with contents: " Search for selected text. " http://vim.wikia.com/wiki/VimTip171 let s:save_cpo = &cpo | set cpo&vim if !exists('g:VeryLiteral') let g:VeryLiteral = 0 endif function! s:VSetSearch(cmd) let old_reg = getreg('"') let old_regtype = getregtype('"') normal! gvy if @@ =~? '^[0-9a-z,_*$' || @@ =~? '^,_*$' && g:VeryLiteral let @/ = @@ else let pat = escape(@@, a:cmd.'\') if g:VeryLiteral let pat = substitute(pat, '\n', '\\n', 'g') else let pat = substitute(pat, '^\_s\+', '\\s\\+', '') let pat = substitute(pat, '\_s\+$', '\\s\\*', '') let pat = substitute(pat, '\_s\+', '\\_s\\+', 'g') endif let @/ = '\V'.pat endif normal! gV call setreg('"', old_reg, old_regtype) endfunction vnoremap * :call VSetSearch('/')// vnoremap # :call VSetSearch('?')?/ vmap * nmap VLToggle :let g:VeryLiteral = !g:VeryLiteral \\| echo "VeryLiteral " . (g:VeryLiteral ? "On" : "Off") if !hasmapto("VLToggle") nmap vl VLToggle endif let &cpo = s:save_cpo | unlet s:save_cpo Explanation The first suggested mapping was: vnoremap * : \let old_reg=getreg('"')let old_regtype=getregtype('"') \gvy/=substitute( \escape(@", '/\.*$^~'\_s\+', '\\_s\\+', 'g') \gV:call setreg('"', old_reg, old_regtype) When in visual mode, pressing * will then perform these commands: : let old_reg=getreg('"')let old_regtype=getregtype('"') gvy /= substitute( escape(@", '/\.*$^~['), '\_s\+', '\\_s\\+', 'g') gV :call setreg('"', old_reg, old_regtype) : enters command mode and deletes (Ctrl-u) the '<,'> range automatically inserted due to the visual selection. The unnamed register (@") is saved and later restored. gvy reselects then yanks the visual selection (copy to @"). /= starts a search, then substitutes the expression register (@=) literally . The result of the following expression is inserted into the command line. escape() inserts a backslash before each /\.*$^~[ character found in @". The / must be escaped because we are using a / command. The other characters need to be escaped because they have a special meaning in a regular expression. substitute() replaces every sequence of one or more whitespace characters (space, tab, newline) with an escaped regular expression that will search for any similar sequence. gV allows the mappings to work in --SELECT-- mode as well as --VISUAL--. Without gV, searching for text in select mode would not move the cursor because the selection is automatically reselected after the mapping. Readable equivalent The principle is to get selection in to use it in search. There are multiple ways to get visually selected text. Here is a simple one, which does not distinguish between v and V selections. function! s:getSelectedText() let l:old_reg = getreg('"') let l:old_regtype = getregtype('"') norm gvy let l:ret = getreg('"') call setreg('"', l:old_reg, l:old_regtype) exe "norm \" return l:ret endfunction vnoremap * :call setreg("/", \ substitute(getSelectedText(), \ '\_s\+', \ '\\_s\\+', 'g') \ )n vnoremap # :call setreg("?", \ substitute(getSelectedText(), \ '\_s\+', \ '\\_s\\+', 'g') \ )n Paste matching text of last search When using ^r/ in INSERT mode what one most of the time wants is to paste the matched text not the regex used to search the text. Example: after using * on a word, ^r/ will paste the word with \< prepended and \> appended, not what we want. Similarly after a visual search we don't want the \V prepended. The following map takes care of these issues: function! Del_word_delims() let reg = getreg('/') " After * i^r/ will give me pattern instead of \ let res = substitute(reg, '^\\<\(.*\)\\>$', '\1', '' ) if res != reg return res endif " After * on a selection i^r/ will give me pattern instead of \Vpattern let res = substitute(reg, '^\\V' , '' , '' ) let res = substitute(res, '\\\\' , '\\', 'g') let res = substitute(res, '\\n' , '\n', 'g') return res endfunction inoremap / =Del_word_delims() cnoremap / =Del_word_delims() For more complicated patterns, it's better to act on the text matched with the last search, using the object. So, you could also accomplish insertion of a search match using maygn`ap in normal mode. I.e. ma to drop a mark to return to later, y to yank the gn object, then `a to jump back where you were (because the yank will leave you on the text copied), finally p to paste. When starting from insert mode, you don't even need a mark: you can use the gi command to start again from where you left off. For example: :inoremap ygngi0 Here, ygn is as before, but gi is used to go back to insert mode in the same place you left off, then 0 inserts the copied text. See also * [[Script:2944|visualstar.vim] Comments Tips related to visual searching (need to merge): *1011 Mappings and commands for visual mode *1151 Search visually This mapping forms a substitute command with the selected text: vnoremap "hy:%s/h//gc Found on Stack Overflow: Vim replace selected text. To auto escape certain characters (e.g. slash and backslash), one can use: vnoremap ""y:%s/=escape(@", '/\')//g